package com.woniuxy01;


import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Test01 {

    static Scanner scanner =new Scanner(System.in);

    public static void main(String[] args) throws Exception {
        boolean flag=true;
        while (flag) {
            System.out.println("欢迎进入系统,选择你要执行的操作");
            System.out.println("1.添加学生");
            System.out.println("2.查询所有学生");
            System.out.println("3.删除学生");
            System.out.println("4.修改学生");
            System.out.println("6.退出系统");
            int choice=scanner.nextInt();
            switch (choice){
                case 1:
                    testaddStudent();
                    break;
                case 2:
                    testgetAllStudents();
                    break;
                case 3:
                    testremoveStudent();
                    break;
                case 4:
                    testmodifyStudent();
                    break;
                case 6:
                    System.out.println("退出系统");
                    flag=false;
                    break;
                default:
                    System.out.println("输入错误，重新选择");
                    break;
            }
        }
    }



    private static void testmodifyStudent() throws Exception {
        Stu stu=new Stu();
        System.out.println("输入学生学号");
        stu.setSid(scanner.nextInt());
        System.out.println("输入学生姓名");
        stu.setSname(scanner.next());
        System.out.println("输入学生密码");
        stu.setPwd(scanner.next());
        System.out.println("输入学生年龄");
        stu.setAge(scanner.nextInt());
        modifyStudent(stu);
    }


    //修改学生
    public static void modifyStudent(Stu stu) throws Exception {
        JdbcUtils.executeUpdate("update stu set sname=?,pwd=?,age=? where sid=?",
                stu.getSname(),stu.getPwd(),stu.getAge(),stu.getSid());

    }
    public static void testremoveStudent() throws Exception{
        System.out.println("输入你要删除的学生的学号");
        int sid=scanner.nextInt();
        removeStudent(sid);
    }

    //删除学生
    public static void removeStudent(int sid) throws Exception{
        JdbcUtils.executeUpdate("delete from stu where sid=?", sid);
    }



    public static void testgetAllStudents() throws Exception {
        getAllStudents().forEach(System.out::println);
    }

    //读取文件中存储的所有学生
    private static List<Stu> getAllStudents() throws Exception {

        return  JdbcUtils.executeQueryList(Stu.class, "select * from stu");
    }

    private static void testaddStudent() throws Exception {
        Stu stu=new Stu();
        System.out.println("输入学生学号");
        stu.setSid(scanner.nextInt());
        System.out.println("输入学生姓名");
        stu.setSname(scanner.next());
        System.out.println("输入学生密码");
        stu.setPwd(scanner.next());
        System.out.println("输入学生年龄");
        stu.setAge(scanner.nextInt());
        addStudent(stu);
    }

    //添加学生，学生数据存储到文件中,每一行存储一个学生对象，属性间使用-做分割:sid-sname-age
    public static void addStudent(Stu stu) throws Exception {
        JdbcUtils.executeUpdate("insert into stu values(?,?,?,?)", stu.getSid(),stu.getSname(),stu.getPwd(),stu.getAge());
    }


}
